package com.shf.common
{
	import com.shf.compoment.MessageTip;
	import com.shf.compoment.MessageUI;
	
	import flash.display.DisplayObject;
	import flash.utils.setTimeout;
	
	import mx.core.Application;
	import mx.core.UIComponent;
	import mx.effects.Effect;
	import mx.effects.Move;
	import mx.events.EffectEvent;
	import mx.managers.PopUpManager;
	
	public class PopHelper
	{
		public function PopHelper()
		{
		}
		
		private static var messageUI:MessageUI = new MessageUI();
		
		public static function popUpMessage(message:String,title:String=null,delayTime:Number = 3000,parent:DisplayObject = null,width:Number=200,height:Number = 170,showEffect:Effect = null,hideEffect:Effect = null):void{
			messageUI.width = width;
			messageUI.height = height;
			if(title)messageUI.title = title;
			if(message)messageUI.content = message;
			parent = getParent(parent);
			messageUI.x = parent.width - width;
			messageUI.y = parent.height - height;
			if(!showEffect)showEffect = getDefaultShowMessageEffect(parent);
			showEffect.target = messageUI;
			showEffect.play();
			PopUpManager.addPopUp(messageUI,parent);
			showEffect.addEventListener(EffectEvent.EFFECT_END,function(event:EffectEvent):void{
				setTimeout(hideMessageUI,delayTime,messageUI,parent,hideEffect);
			});
			
		}
		private static var messageTip:MessageTip;
		public static function popTipMessage(message:String,delayTime:Number = 3000,parent:DisplayObject = null):void{
			if(!messageTip)messageTip = new MessageTip();
			messageTip.message = message;
			messageTip.close = hidePopTipMessage;
			parent = getParent(parent);
			messageTip.x = parent.width - messageTip.width;
			messageTip.y = parent.height - messageTip.height;
			trace(messageTip.x);
			trace(messageTip.y);
			var showEffect:Effect = getDefaultShowMessageEffect(parent);
			showEffect.target = messageTip;
			showEffect.play();
			PopUpManager.addPopUp(messageTip,parent);
			showEffect.addEventListener(EffectEvent.EFFECT_END,function(event:EffectEvent):void{
				setTimeout(hidePopTipMessage,delayTime,messageTip,parent);
			});
		}
		
		private static function hidePopTipMessage(messageTip:UIComponent,parent:DisplayObject):void{
			var hideEffect:Effect = getDefaultHideMessageEffect(parent); 
				hideEffect.target = messageTip;
				hideEffect.play();
				hideEffect.addEventListener(EffectEvent.EFFECT_END,function(event:EffectEvent):void{
					PopUpManager.removePopUp(messageTip);
			});
		}
		
		private static function getDefaultShowMessageEffect(parent:DisplayObject):Effect{
			var move:Move = new Move();
			move.yFrom = parent.height;
			move.yTo = parent.height - messageUI.height;
            move.duration=1500;
            return move;
		}
		
		private static function getDefaultHideMessageEffect(parent:DisplayObject):Effect{
			var move:Move = new Move();
			move.yFrom = messageUI.y;
			move.yTo = parent.height;
			move.duration = 1500;
			return move;
		} 
		
		private static function hideMessageUI(messageUI:UIComponent,parent:DisplayObject = null,hideEffect:Effect = null):void{
			if(!hideEffect)hideEffect = getDefaultHideMessageEffect(parent); 
			hideEffect.target = messageUI;
			hideEffect.play();
			hideEffect.addEventListener(EffectEvent.EFFECT_END,function(event:EffectEvent):void{
				PopUpManager.removePopUp(messageUI);
			});
		}
		
		private static function getParent(parent:DisplayObject):DisplayObject{
			if(!parent)parent = Application.application as DisplayObject; 
			return parent;
		}
		
		private static function getDefaultShowWindowUIEffect(window:UIComponent,parent:DisplayObject):Effect{
			var effect:Effect = null;
			var num:Number = Math.round(Math.random()*10);
			trace(num);
			if(num%5 == 0){
				if(num%2 == 0){
					
				}else{
					
				}
				var move:Move = new Move();
				move.xFrom = 0;
				move.yFrom = parent.height;
				move.xTo = parent.width / 2 - window.width/2;
				move.yTo = parent.height / 2 - window.height/2;
				effect = move;
			}else if(num%3 == 0){
				var move:Move = new Move();
				move.xFrom = 0;
				move.yFrom = 0;
				move.xTo = parent.width / 2 - window.width/2;
				move.yTo = parent.height / 2 - window.height/2;
				effect = move;	
			}else if(num % 2 == 0){
				var move:Move = new Move();
				move.xFrom = parent.width;
				move.yFrom = 0;
				move.xTo = parent.width / 2 - window.width/2;
				move.yTo = parent.height / 2 - window.height/2;
				effect = move;	
			}else{
				var move:Move = new Move();
				move.xFrom = parent.width;
				move.yFrom = parent.height;
				move.xTo = parent.width / 2 - window.width/2;
				move.yTo = parent.height / 2 - window.height/2;
				effect = move;
			}
			return effect;
		}
		
		public static function popCompoment(window:UIComponent,parent:DisplayObject = null,mode:Boolean = false,showEffect:Effect = null,delayTime:Number = -1):void{
			parent = getParent(parent);
			if(!showEffect)showEffect = getDefaultShowWindowUIEffect(window,parent);
			showEffect.target = window;
			showEffect.play();
			PopUpManager.addPopUp(window,parent,mode);
		}
	}
}